package com.dalei.vdserver.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.dalei.vdserver.model.Task;

public class TaskDaoImpl extends JdbcDaoSupport implements TaskDao {

	@Override
	@SuppressWarnings("unchecked")
	public List<Task> getTasks() {
		// TODO Auto-generated method stub
		final String sql = "SELECT * FROM vds_tasks";
		List<Task> tasks = null;
		try {
			tasks = getJdbcTemplate().query(sql, new RowMapper() {

				@Override
				public Object mapRow(ResultSet rs, int rowNum)
						throws SQLException {
					// TODO Auto-generated method stub
					Task task = new Task();
					task.setFid(rs.getInt("_fid"));
					task.setVehicleInformationId(rs
							.getInt("_vehicleInformationId"));
					task.setDeviceId(rs.getInt("_deviceId"));
					task.setLocked(rs.getBoolean("_isLocked"));
					Timestamp timestamp = rs.getTimestamp("_timestamp");
					Date date = new Date(timestamp.getTime());
					task.setTimestamp(date);
					return task;
				}
			});
		} catch (DataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		return tasks;
	}

	@Override
	public Task getTaskById(int fid) {
		// TODO Auto-generated method stub
		final String sql = "SELECT * FROM vds_tasks WHERE _fid = ?";
		Task task = null;
		try {
			task = (Task) getJdbcTemplate().queryForObject(sql,
					new Object[] { fid }, new RowMapper() {

						@Override
						public Object mapRow(ResultSet rs, int rowNum)
								throws SQLException {
							// TODO Auto-generated method stub
							Task t = new Task();
							t.setFid(rs.getInt("_fid"));
							t.setVehicleInformationId(rs
									.getInt("_vehicleInformationId"));
							t.setDeviceId(rs.getInt("_deviceId"));
							t.setLocked(rs.getBoolean("_isLocked"));
							Timestamp timestamp = rs.getTimestamp("_timestamp");
							Date date = new Date(timestamp.getTime());
							t.setTimestamp(date);
							return t;
						}
					});
		} catch (IncorrectResultSizeDataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		} catch (DataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		return task;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Task> getTasksByDeviceId(int deviceId) {
		// TODO Auto-generated method stub
		final String sql = "SELECT * FROM vds_tasks "
				+ "WHERE (_deviceId = ? OR _deviceId = NULL) AND _isLocked = 0 "
				+ "ORDER BY _timestamp";
		List<Task> tasks = null;
		try {
			tasks = getJdbcTemplate().query(sql, new Object[] { deviceId },
					new RowMapper() {

						@Override
						public Object mapRow(ResultSet rs, int rowNum)
								throws SQLException {
							// TODO Auto-generated method stub
							Task task = new Task();
							task.setFid(rs.getInt("_fid"));
							task.setVehicleInformationId(rs
									.getInt("_vehicleInformationId"));
							task.setDeviceId(rs.getInt("_deviceId"));
							task.setLocked(rs.getBoolean("_isLocked"));
							Timestamp timestamp = rs.getTimestamp("_timestamp");
							Date date = new Date(timestamp.getTime());
							task.setTimestamp(date);
							return task;
						}
					});
		} catch (DataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		return tasks;
	}

	@Override
	public int updateTask(Task task) {
		// TODO Auto-generated method stub
		final String sql = "UPDATE vds_tasks SET _isLocked = ? WHERE _fid = ?";
		int rowAffected = 0;
		try {
			if (task.isLocked()) {
				rowAffected = getJdbcTemplate().update(sql,
						new Object[] { 1, task.getFid() });
			} else {
				rowAffected = getJdbcTemplate().update(sql,
						new Object[] { 0, task.getFid() });
			}
		} catch (DataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return -1;
		}
		return rowAffected;
	}

	@Override
	public int removeTask(int fid) {
		// TODO Auto-generated method stub
		final String sql = "DELETE FROM vds_tasks WHERE _fid = ?";
		int rowAffected = 0;
		try {
			rowAffected = getJdbcTemplate().update(sql, new Object[] { fid });
		} catch (DataAccessException e) {
			// TODO: handle exception
			e.printStackTrace();
			return -1;
		}
		return rowAffected;
	}
}
